﻿/*******************************************************************
* Copyright(c)
* 文件名称: BLEExample.cs
* 简要描述:
* 创建日期: #CreateTime# 
* 作者: QianXi
* 邮箱: 2470460089@qq.com
******************************************************************/

using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class BLEExample : MonoBehaviour
{
    public GameObject DevicePre;
    public GameObject ScanPage;
    public GameObject DeviceInfoPage;
    public GameObject CharacterInfoPage;
    public Tip TipPage;
    public Transform DeviceParent;
    Dictionary<string, BLEScanDevice> scanDeviceDic = new Dictionary<string, BLEScanDevice>();

    private void Awake()
    {
        BLEManager.Instance.BlueInit(BlueInitFinish,true, A2DPListen);
    }
    public Text A2DPText;
    public void A2DPListen(bool isConnected,BLEDeviceInfo bLEDeviceInfo)
    {
        if(!isConnected)
        {
            A2DPText.text = "  A2DP(连接的音频蓝牙) : 未连接";
        }
        else
        {
            A2DPText.text = "  A2DP(连接的音频蓝牙) : " + bLEDeviceInfo.DeviceName + " Address:" + bLEDeviceInfo.MacAddress;
        }
    }
    public void BlueInitFinish()
    {
        Debug.Log("蓝牙模块初始化完成!");
    }
    public void StartScan()
    {
        //BLEManager.Instance.StartScan(new string[1] { "6666" }, ScanDeviceEvent);
        BLEManager.Instance.StartScan(null, ScanDeviceEvent);
    }
    public void ScanDeviceEvent(BLEDeviceInfo bLEScanDeviceInfo)
    {
        if(scanDeviceDic.ContainsKey(bLEScanDeviceInfo.MacAddress))
        {
            scanDeviceDic[bLEScanDeviceInfo.MacAddress].UpdateInfo(bLEScanDeviceInfo);
        }
        else
        {
            BLEScanDevice bLEScanDevice = Instantiate(DevicePre, DeviceParent).AddComponent<BLEScanDevice>();
            bLEScanDevice.Init(bLEScanDeviceInfo,this);
            scanDeviceDic.Add(bLEScanDeviceInfo.MacAddress, bLEScanDevice);
        }
    }
    public void StopScan()
    {
        for(int i = DeviceParent.childCount-1;i>=0;i--)
        {
            DestroyImmediate(DeviceParent.GetChild(i).gameObject);
        }
        scanDeviceDic.Clear();
        BLEManager.Instance.StopScan();
    }
    public void ShowConnectTip()
    {
        StopScan();
        TipPage.ShowTip("正在连接,请稍候...",TipType.NoBtn);
    }
    public void ConnectListern(bool isConnect)
    {
        if(isConnect)
        {
            //连接成功,跳转页面
            ScanPage.SetActive(false);
            TipPage.gameObject.SetActive(false);
            DeviceInfoPage.SetActive(true);
            deviceInfo = DeviceInfoPage.GetComponent<DeviceInfo>();
        }
        else
        {
            //连接失败
            TipPage.ShowTip("连接失败,请重试!", TipType.OneBnt);
        }
    }
    public void DeviceInfoBackScan()
    {
        DeviceInfoPage.SetActive(false);
        BLEManager.Instance.DisConnect();
        StartScan();
        ScanPage.SetActive(true);
    }
    public void DeviceInfoToCharacterInfo(CharacteristicInfo characteristicInfo)
    {
        CharacterInfoPage.SetActive(true);
        DeviceInfoPage.SetActive(false);
        CharacterInfoPage.GetComponent<CharacterInfo>().Init(characteristicInfo);
    }
    public void CharacterInfoToDeviceInfo()
    {
        CharacterInfoPage.SetActive(false);
        DeviceInfoPage.SetActive(true);
    }
    DeviceInfo deviceInfo = null;
    public void DiscoveryServices(string serviceUUID)
    {
        deviceInfo.OnDiscoveryService(serviceUUID);
    }
    public void DiscoveryCharacteristic(CharacteristicInfo characterInfo)
    {
        deviceInfo.OnDiscoveryCharacteristic(characterInfo);
    }
    private void OnApplicationPause(bool pause)
    {
        BLEManager.Instance.OnApplicationPause(pause);
    }
}
